package com.jdcjk.project.main.presenter;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.project.account.model.entity.Result;
import com.jdcjk.project.login.model.entity.AppInfoResult;
import com.jdcjk.project.main.model.MainModel;
import com.jdcjk.project.main.model.entity.CarListInfo;
import com.jdcjk.project.main.model.entity.TerminalInfoListResult;
import com.jdcjk.project.main.model.entity.WarningInfoListResult;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by dong on 2016/12/12.
 * 主程序中介类
 */
public class MainPresenter extends BasePresenter<MainModel> {
    public MainPresenter(Context context) {
        super(context);
    }

    @Override
    public MainModel bindModel() {
        return new MainModel(getContext());
    }

    public List<CarListInfo> getCarListInfo() {
        return getModel().getCarListInfo();
    }

    /**
     * 获取设备报警信息
     *
     * @param url                请求url
     * @param account            设备id
     * @param onUIThreadListener 回调
     */
    public void getCarWarmingInfo(String url, String account, final OnUIThreadListener<WarningInfoListResult> onUIThreadListener) {
        getModel().getCarWarmingInfoList(url, account, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }


            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                WarningInfoListResult warningInfoListResult = gson.fromJson(s, WarningInfoListResult.class);
                onUIThreadListener.onResponse(warningInfoListResult);
            }
        });
    }

    /**
     * 获得app组信息
     *
     * @param url                请求地址
     * @param aId                App Id
     * @param _api_key           API Key
     * @param onUIThreadListener 在主线程的回调
     */
    public void getAppInfo(String url, String aId, String _api_key, final OnUIThreadListener<AppInfoResult> onUIThreadListener) {
        getModel().getAppGroupInfo(url, aId, _api_key, new StringCallback() {

            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                AppInfoResult appInfoResult = gson.fromJson(s, AppInfoResult.class);
                onUIThreadListener.onResponse(appInfoResult);
            }
        });
    }

    /**
     * 删除一条报警信息
     *
     * @param url                请求url
     * @param id                 信息id
     * @param onUIThreadListener onUIThreadListener
     */
    public void deleteWarningInfo(String url, String id, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().deleteWarningInfo(url, id, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                Result result = gson.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }

    /**
     * 获取用户终端列表
     *
     * @param url                请求url
     * @param account            用户账号
     * @param onUIThreadListener 回调
     */
    public void getTerminalForUser(String url, String account, final OnUIThreadListener<TerminalInfoListResult> onUIThreadListener) {
        getModel().getTerminalForUser(url, account, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                TerminalInfoListResult terminalInfoListResult = json.fromJson(s, TerminalInfoListResult.class);
                onUIThreadListener.onResponse(terminalInfoListResult);
            }
        });
    }

    /**
     * 绑定终端
     *
     * @param url                请求url
     * @param simNum             终端编号
     * @param account            用户账号
     * @param onUIThreadListener 回调
     */
    public void bindTerminal(String url, String account, String simNum, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().bindTerminal(url, account, simNum, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                Result bindCarResult = gson.fromJson(s, Result.class);
                onUIThreadListener.onResponse(bindCarResult);
            }
        });
    }

    /**
     * 解绑终端
     *
     * @param url                请求url
     * @param simNum             终端编号
     * @param account            用户账号
     * @param onUIThreadListener 回调
     */
    public void unbindTerminal(String url, String account, String simNum, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().unbindTerminalForUser(url, account, simNum, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                Result unbindResult = gson.fromJson(s, Result.class);
                onUIThreadListener.onResponse(unbindResult);
            }
        });
    }

    /**
     * 获取未读的信息数量
     *
     * @param url                请求UrL
     * @param uid                用户id
     * @param onUIThreadListener 回调
     */
    public void getUnreadMsgNum(String url, String uid, final OnUIThreadListener<String> onUIThreadListener) {
        getModel().getUnreadMsgNum(url, uid, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }


            @Override
            public void onResponse(String s, int i) {
                onUIThreadListener.onResponse(s);
            }
        });
    }

    /**
     * 修改用户所有报警信息状态(将该设备所有报警信息都改为已读)
     *
     * @param url                请求UrL
     * @param terminalIDs        终端id，多个用,隔开
     * @param onUIThreadListener 回调
     */
    public void updateMsgStatus(String url, String terminalIDs, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().updateMsgStatus(url, terminalIDs, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                Result result = gson.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }
}
